package  main.java.leetcode.editor.cn;
//2023-07-04 15:22:26
//给定一个有序整数数组，元素各不相同且按升序排列，编写一个算法，创建一棵高度最小的二叉搜索树。示例: 给定有序数组: [-10,-3,0,5,9], 一个可能
//的答案是：[0,-3,9,-10,null,5]，它可以表示下面这个高度平衡二叉搜索树：           0          / \        -3 
//  9        /   /      -10  5 Related Topics 树 二叉搜索树 数组 分治 二叉树 👍 156 👎 0

class MinimumHeightTreeLcci {
    public static void main(String[] args) {
        //创建该题目的对象方便调用
        Solution solution = new MinimumHeightTreeLcci().new Solution();
    }
    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;
        TreeNode(int x) { val = x; }
    }
    //leetcode submit region begin(Prohibit modification and deletion)
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public TreeNode sortedArrayToBST(int[] nums) {
        return helper(nums,0,nums.length-1);
    }

    private TreeNode helper(int[] nums,int low,int high){

        if(low > high){
            return null;
        }

        int mid = (low+high)/2;
        TreeNode node = new TreeNode(nums[mid]);

        node.left = helper(nums,low,mid-1);

        node.right = helper(nums,mid+1,high);

        return node;
    }
}
//leetcode submit region end(Prohibit modification and deletion)

}
